﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace BaseHelper.Base
{
    public class ThreadHelper
    {
        private static object locker = new object();
        public static ConcurrentDictionary<int, string> sl = new ConcurrentDictionary<int, string>();

        public static int Count
        {
            get { return sl.Count; }
        }

        public static string List
        {
            get 
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("");
                
                lock (locker)
                {
                    foreach (int id in sl.Keys)
                    { 
                        sb.Append(id.ToString() +"\t" + sl[id].ToString() +"\r\n");
                    }

                }
                return sb.ToString();

            }
        }
        //ThreadHelper.Add(th.ManagedThreadId, th.Name);
        public static void Add(int id,string name)
        {
            
            lock (locker)
            {
                if(!sl.ContainsKey(id))
                    sl[id]=name;
            }
        }

        public static void Remove(int id)
        {
            lock (locker)
            {
                string tmp;
                if (sl.ContainsKey(id) && sl.TryRemove(id, out tmp))
                {
                    tmp = null;
                }
            }
        }
    }
}
